package com.huosu.utils;

import cn.hutool.core.util.RuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: miner-shield-web
 * @description:
 * @author: changlin
 * @create: 2019-07-31 16:19
 **/
@Slf4j
public class ShellUtil {

    /**
     * 解决了 参数中包含 空格和脚本没有执行权限的问题
     *
     * @param scriptPath 脚本路径
     * @param wait       是否等待返回结果
     * @param para       参数数组
     */
    public static boolean execShell(String scriptPath, boolean wait, String... para) {
        try {
            String[] cmd = new String[]{scriptPath};
            //为了解决参数中包含空格
            cmd = ArrayUtils.addAll(cmd, para);
//            log.info("execShell {} ", cmd);

            //解决脚本没有执行权限
//            ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", scriptPath);
//            Process process = builder.start();
//            process.waitFor();
//            RuntimeUtil.exec(cmd);
            if (wait) {
//                Process ps = RuntimeUtil.exec(cmd);
//                ps.waitFor();
//
//                BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
//                StringBuffer sb = new StringBuffer();
//                String line;
//                while ((line = br.readLine()) != null) {
//                    sb.append(line).append("\n");
//                }

                String re = RuntimeUtil.execForStr(cmd);
                //执行结果
//                String result = sb.toString();
//                log.info("execShell {} {}  result:{} ", scriptPath, para, result);
            } else {
                RuntimeUtil.exec(cmd);
            }

            return true;
        } catch (Exception e) {
            log.error("execShell {} {}  Exception:{} ", scriptPath, para, e.getMessage());
            return false;
        }
    }

    public static boolean execCommand(String command) {
        try {
            RuntimeUtil.exec(command);
        } catch (Exception e) {
            log.error("execCommand {}  Exception:{} ", command, e.getMessage());
            return false;
        }
        return true;
    }

    public static String execShellWithResult(String cmd) {
        try {
            String ps = RuntimeUtil.execForStr(cmd);
//            ps.waitFor();
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
//            StringBuffer sb = new StringBuffer();
//            String line;
//            while ((line = br.readLine()) != null) {
//                sb.append(line).append("\n");
//            }
//            //执行结果
//            String result = sb.toString();
//            log.info("execShell {}   result:{} ", cmd, result);
            return ps;
        } catch (Exception e) {
            log.error("execShell {}  Exception:{} ", cmd, e.getMessage());
            return "";
        }
    }

//    public static List<String> execNping(String cmd) {
//        log.info("execNping {} ", cmd);
//        List<String> list = new LinkedList<String>();
//        try {
//            Process ps = RuntimeUtil.exec(cmd);
//            ps.waitFor();
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
//
//            String line;
//            while ((line = br.readLine()) != null) {
//                list.add(line);
//            }
//
//            if (list != null && list.size() > 4) {
//                list = list.subList(list.size() - 3, list.size() - 1);
//            }
//            return list;
//        } catch (Exception e) {
//            log.error("execNping {}  Exception:{} ", cmd, e.getMessage());
//            return list;
//        }
//    }
//
//    public static List<String> nping(String cmd) {
//        log.info("cmd {} ", cmd);
//        List<String> list = new LinkedList<String>();
//        try {
//            list = RuntimeUtil.execForLines(cmd);
//        } catch (Exception e) {
//            log.info("cmd {} ", cmd);
//        }
//        return list;
//    }
}